/*
 * @(#)DefaultListInstanceFactory.java $version 2010. 8. 11
 *
 * Copyright 2007-2010 NHN Corp. All rights Reserved. 
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nhncorp.usf.core.ognl;

import java.util.ArrayList;

/**
 * The Class DefaultListInstanceFactory.
 *
 * @author MiddleWare Platform Development Team
 */
class DefaultListInstanceFactory implements DefaultInstanceFactory {
    /**
     * {@inheritDoc}
     */
    public Object getInstance(Class<?> type) throws Exception {
        return new ExpandingArrayList<Object>();
    }

    /**
     * The Class ExpandingArrayList.
     */
    private static class ExpandingArrayList<E> extends ArrayList<E> {
        private static final long serialVersionUID = -8476563790569171198L;

		@Override
		public E get(int index) {
			if (size() <= index) {
				return null;
			} else {
				return super.get(index);
			}
		}

        @Override
        public E set(int index, E element) {
            if (size() <= index) {
                ensureCapacity(index + 1);

                while (size() <= index) {
                    add(null);
                }

            }

            return super.set(index, element);
        }
    }
}
